<?php

class grid{
	var $pageSize ;
	var $pageNo ;
	var $pageCount ;
	var $totalCount ;
	var $thisCount ;
	var $result ;
	var $columns = array() ;
	var $fields = array() ;
	var $navtable = "";
	var $navdiv = "" ;
	var $orderby = "";
	var $whereCondition = "";
	var $sqlstr = "";
	var $countSql = "";
	var $navType = 0 ;
	var $debug = false ;

	var $db_id = "default";

	function grid(){
		$this->pageSize		= 16;
		$this->pageNo		= 1;
		$this->pageCount	= 1;
		$this->totalCount	= 0;
		$this->thisCount	= 0;
	}

	function setOrdstr($ordstr){
		$this->orderby = $ordstr ;
	}

	function setNavType($type){
		$this->navType = $type ;
	}

	function setDb($db_id){
		if(!empty($db_id)){
			$this->db_id = $db_id ;
		}
	}
	
	function footer_table($object,$row_1,$row_2){
		$navstr  = "";
		$navstr .= "<table class='table_nav' cellspacing=0 cellpadding=0>";
		$navstr .= "<tr class='tr_foot'>";
		$navstr .= "<td class='td_nav_l'>($row_1-$row_2)/$object->totalCount</td>";//($row_1-$row_2)/$object->totalCount
		$navstr .= "<td class='td_nav_c'>";
	
		$no1 = $object->pageNo - 5 ;
		$no2 = $object->pageNo + 5 ;
	
		if($object->pageCount > 1){
			for($n=$no1;$n<=$no2;$n++){
				if($n < 0){
					continue ;
				}
				if($n >= $object->pageCount){
					continue ;
				}
				$no = $n + 1 ;
				if($n == $object->pageNo){
					$navstr .= "[".$no."]&nbsp;";
				}else{
					$navstr .= "<A HREF=\"javascript:pageNav('$n')\">[".($no)."]</A>&nbsp;";
				}
			}
		}
	
		$navstr .= "</td>";
		$navstr .= "<td class='td_nav_r'>";
		$current_pageNo = $object->pageNo ;
		if($object->navType == 0){
			if($object->pageCount <= 1){
				//
			}else{
				if($object->pageNo == 0){
					$navstr .= "<A HREF=\"#\"><img src='../images/first1.gif' border=0></img></A>&nbsp;";
				}else{
					$navstr .= "<A HREF=\"javascript:pageNav('0')\"><img src='../images/first.gif' border=0></img></A>&nbsp;";
				}
				if($object->pageNo == 0){
					$navstr .= "<A HREF='#'><img src='../images/previous1.gif' border=0></img></A>&nbsp;";
				}else{
					$navstr .= "<A HREF=\"javascript:pageNav('".($object->pageNo-1)."')\"><img src='../images/previous.gif' border=0></img></A>&nbsp;";
				}
				
			//	$navstr .= "第<input type='text' name='page_num' id='page_num'  value='".($current_pageNo+1)."'>页/共".$object->pageCount."页" ;
				if($object->pageNo == ($object->pageCount - 1)){
					$navstr .= "<A HREF='#'><img src='../images/next1.gif' border=0></img></A>&nbsp;";
				}else{
					$navstr .= "<A HREF=\"javascript:pageNav('".($object->pageNo+1)."')\"><img src='../images/next.gif' border=0></img></A>&nbsp;";
				}
				if($object->pageNo == ($object->pageCount - 1)){
					$navstr .= "<A HREF=\"#\"><img src='../images/last1.gif' border=0></img></A>&nbsp;";
				}else{
					$navstr .= "<A HREF=\"javascript:pageNav('".($object->pageCount - 1)."')\"><img src='../images/last.gif' border=0></img></A>&nbsp;";
				}
			}
		}
	
		$navstr .= "</td>";
		$navstr .= "</tr>";
		$navstr .= "</table>";
		$navstr .= "";
	
		return $navstr ;
	
	}
	
	function footer_div($object){
		$html  = "" ;
		$html .= "<div class='Yeqian'><ul>" ;
		
		$html .= "<li>";
		if($object->pageNo == 0){
			$html .= "<A HREF=\"#\"><img src='/system/images/first1.gif' border=0></img></A>&nbsp;";
		}else{
			$html .= "<A HREF=\"javascript:pageNav('0')\"><img src='/system/images/first.gif' border=0></img></A>&nbsp;";
		}
		$html .= "</li>";
		
		$html .= "<li>";
		if($object->pageNo == 0){
			$html .= "<A HREF='#'><img src='/system/images/previous1.gif' border=0></img></A>&nbsp;";
		}else{
			$html .= "<A HREF=\"javascript:pageNav('".($object->pageNo-1)."')\"><img src='/system/images/previous.gif' border=0></img></A>&nbsp;";
		}
		$html .= "</li>";

		$html .= "<li> 第 <input type='text' name='page_num' id='page_num' current_page='".$object->pageNo."' max_page='".$object->pageCount."' value='".($object->pageNo+1)."' onblur='pageNoJumpTo(this,1)' onkeypress='pageNoJumpTo(this,2)' style='height:12px;line-height:12px;'/> 页</li>" ;
		//onkeyup='revisePageNo(this)' onkeydown='revisePageNo(this)'
		$html .= " / <li>共 ".$object->pageCount." 页</li>&nbsp;&nbsp;" ;
		
		$html .= "<li>";
		if($object->pageNo == ($object->pageCount - 1)){
			$html .= "<A HREF='#'><img src='/system/images/next1.gif' border=0></img></A>&nbsp;";
		}else{
			$html .= "<A HREF=\"javascript:pageNav('".($object->pageNo+1)."')\"><img src='/system/images/next.gif' border=0></img></A>&nbsp;";
		}
		$html .= "</li>";

		$html .= "<li>";
		if($object->pageNo == ($object->pageCount - 1)){
			$html .= "<A HREF=\"#\"><img src='/system/images/last1.gif' border=0></img></A>&nbsp;";
		}else{
			$html .= "<A HREF=\"javascript:pageNav('".($object->pageCount - 1)."')\"><img src='/system/images/last.gif' border=0></img></A>&nbsp;";
		}
		$html .= "</li>";

		$html .= "</ul></div>" ;
		
		return $html ;
	}

	function init($sqlstr,$page_no,$page_size){
		
		global $configs;
		global $SCRIPT_NAME;
		
		$this->pageSize		= $page_size;
		$this->pageNo		= $page_no;
		$this->result		= array();

		if($this->pageSize > 0){
			if(empty($this->countSql)){
				$this->countSql = "select count(0) as sl from (" . $sqlstr . ") t";
			}
			if($this->debug){
				echo "count sqlstr is: <br> " . $this->countSql . "<br/><br>" ;
			}
			$this->totalCount = query_col00($this->countSql,$this->db_id) ;
		}
		
		if($this->pageSize > 0){
			print $this->totalCount ;
			$this->pageCount = ceil($this->totalCount * 1.0 / $this->pageSize) ;
			$this->pageCount = $this->pageCount == 0?1:$this->pageCount ;
		}

		if($this->pageNo >= $this->pageCount && $this->pageCount > 0){
			$this->pageNo = 0 ;
		}
		
		$query = $sqlstr ;
		if(isset($this->orderby) && $this->orderby != ""){
			$query = $query . " order by ". $this->orderby ;
		}
		if($this->pageSize > 0){
			$begin = $this->pageNo * $this->pageSize ;
			$query = $query . " limit $begin," . $this->pageSize ;
		}

		$this->sqlstr = $query ;
		if($this->debug){
			echo "<br>".$this->sqlstr . "<br><br/>" ;
		}
		// ECHO $query;
		$this->result = query_result($query,MYSQL_ASSOC,$this->db_id);

		$this->thisCount = count($this->result) ;
		if($this->pageSize <= 0){
			$this->totalCount = count($this->result);
			$this->pageSize = $this->totalCount ;
		}

		if(count($this->result) > 0){
			while(list($key,$value) = each($this->result[0])){
				$column = strtolower($key) ;
				array_push($this->columns , $column);
				$this->fields[$column] = $key ;
			}
		}

		$row_1 = $this->pageNo * $this->pageSize + 1 ;
		$row_2 = $row_1 + $this->pageSize - 1 ;

		if($row_2 > $this->totalCount){
			$row_2 = $this->totalCount ;
		}

		$this->navtable = $this->footer_table($this,$row_1,$row_2) ;
		$this->navdiv = $this->footer_div($this) ;
		return ;
	}
	
	 

	function getSum($column){
		$total = 0;
		for($i=0;$i<$this->getFetchRows();$i++){
			if($this->get($i,$column) == ""){
				continue ;
			}
			$total += $this->get($i,$column) * 1 ;
		}
		return $total ;
	}

	function getTime($rowIndex,$column){
		$value = $this->get($rowIndex,$column);
		if($value == null || $value == ""){
			return "";
		}
		return substr($value,0,16);
	}

	function getShortTime($rowIndex,$column){
		$value = $this->get($rowIndex,$column);
		if($value == null || $value == ""){
			return "";
		}
		return substr($value,5,11);
	}

	function getDate($rowIndex,$column){
		$value = $this->get($rowIndex,$column);
		if($value == null || $value == ""){
			return "";
		}
		return substr($value,0,10);
	}

	function get($rowIndex,$column){
		if($this->totalCount == 0 || $this->totalCount < $rowIndex){
			return "out of row index $rowIndex [".$this->sqlstr."]";
		}
		$name = $this->getFieldName($column);
		if($name == ""){
			return "unknow column $column";
		}
		return $this->result[$rowIndex][$name];
	}

	function getLine($index){
		if(count($this->result) > $index){
			return $this->result[$index] ;
		}
		return false ;
	}

	function get_column_into_matrix($column){
		$matrix = array();
		for($i=0;$i<$this->getFetchRows();$i++){
			$one = $this->get($i,$column) ;
			if($one != ""){
				$matrix[$i] = $one;
			}
		}
		return $matrix ;
	}
	
	function getColumnIntoList($column){
		return $this->get_column_into_matrix($column) ;
	}

	function getCol($rowIndex,$colIndex){
		$column = $this->getFieldNameByIndex($colIndex) ;
		return $this->get($rowIndex,$column);;
	}

	function getFieldNameByIndex($colIndex){
		$matrix = array_values($this->fields) ;
		if($colIndex >= count($matrix)){
			return "out of index $colIndex";
		}
		return $matrix[$colIndex];
	}

	function columns(){
		return $this->columns ;
	}

	function getFieldName($name){
		$temp = strtolower($name) ;
		if(array_key_exists($temp,$this->fields)){
			return $this->fields[$temp];
		}
		return "";
	}

	function getFetchRows(){
		return $this->thisCount ;
	}
	
	public function getFetchResult(){
		return $this->result ;
	}

	function get_span(){
		
	}

	function getNavTable(){
		return $this->navtable ;
	}
	
	function getNavDiv(){
		return $this->navdiv ;
	}

	function showEmptyRows($n){
		echo $this->getEmptyRows($n) ;
	}
	
	function setDebug($debug){
		$this->debug = $debug ;
	}

	function getEmptyRows($n){
		global $configs ;
		$str = '';
		$len = $this->pageSize - $this->thisCount ;

		for($i=0;$i<$len;$i++){
			$css = ($this->thisCount + $i) % 2 == 0 ? mget($configs,"grid_odd_row_css",0,"row_0") : mget($configs,"grid_event_row_css",0,"row_1") ;
			$event = mget($configs,"grid_empty_row_mouse_event",' onmouseover="mouseOverRow(this)"') ;

			$str .= "<tr class='".$css."' $event>";
			for($t=0;$t<$n;$t++){
				$str .= "<td class='cell'></td>";
			}
			$str .= '</tr>'."\n";
		}
		return $str ;
	}

	function setCountSql($sql){
		$this->countSql = $sql ;
	}

	/**
	 * @return mixed
	 */
	public function getResult()
	{
		return $this->result;
	}

	/**
	 * @param mixed $result
	 */
	public function setResult($result)
	{
		$this->result = $result;
	}


}

